package edu.jabs.patientsCentral.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import edu.jabs.patientsCentral.domain.Patient;

/**
 * Dialogue to display information from a patient
 */
public class ShowPatientInformationDialogue extends JDialog implements ActionListener
{
    // -----------------------------------------------------------------
    // Constants
    // -----------------------------------------------------------------
    private final static String CLOSE = "Close";
    private final static String SAVE_CHANGES = "Save Changes";

    // -----------------------------------------------------------------
    // Fields
    // -----------------------------------------------------------------

    /**
     * It is a reference to the main window
     */
    private PatientsCentralGUI principal;

    /**
     * The patient from which to display information
     */
    private Patient patient;

    // -----------------------------------------------------------------
    // GUI Fields
    // -----------------------------------------------------------------

    /**
     * Panel to display patient information
     */
    private PatientInformationPanel informationPanel;

    /**
     * Button to close the dialogue
     */
    private JButton buttonClose;

    /**
     * Button to update the medical information
     */
    private JButton buttonSaveChanges;

    // -----------------------------------------------------------------
    // Constructors
    // -----------------------------------------------------------------

    /**
     * Dialogue Constructor
     * @param window Reference to the main window - window!=null
     * @param thePatient The patient from which to display information - thePatient!=null
     */
    public ShowPatientInformationDialogue( PatientsCentralGUI window, Patient thePatient )
    {
        super( window, true );
        principal = window;
        patient = thePatient;
        setLayout( new GridBagLayout( ) );
        setPreferredSize( new Dimension( 298, 307 ) );
        setTitle( "Patient Search" );
        setResizable( false );

        // Panel to enter information
        informationPanel = new PatientInformationPanel( );
        GridBagConstraints gbc = new GridBagConstraints( );
        informationPanel.changeInformationComboHospital( principal.getHospitalsCentral( ) );
        informationPanel.disableComponents( );
        informationPanel.enableMedicalInformationArea( );
        informationPanel.showPatientInformation( patient );
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridheight = 4;
        gbc.fill = GridBagConstraints.BOTH;
        add( informationPanel, gbc );

        // Panel with the close and update buttons
        JPanel buttonsPanel = new JPanel( );
        buttonsPanel.setLayout( new GridBagLayout( ) );
        gbc = new GridBagConstraints( );
        gbc.gridx = 1;
        gbc.gridy = 0;
        buttonClose = new JButton( CLOSE );
        buttonClose.setActionCommand( CLOSE );
        buttonClose.addActionListener( this );
        buttonsPanel.add( buttonClose, gbc );

        gbc = new GridBagConstraints( );
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.insets = new Insets( 0, 0, 0, 10 );
        buttonSaveChanges = new JButton( SAVE_CHANGES );
        buttonSaveChanges.setActionCommand( SAVE_CHANGES );
        buttonSaveChanges.addActionListener( this );
        buttonsPanel.add( buttonSaveChanges, gbc );

        gbc = new GridBagConstraints( );
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.BOTH;
        add( buttonsPanel, gbc );

        pack( );

    }

    // -----------------------------------------------------------------
    // Methods
    // -----------------------------------------------------------------

    /**
     * Handling button events
     * @param e Event generated by the action - e != null.
     */
    public void actionPerformed( ActionEvent e )
    {
        String command = e.getActionCommand( );

        if( command.equals( CLOSE ) )
        {
            dispose( );
        }
        else if( command.equals( SAVE_CHANGES ) )
        {
            String newInformation = informationPanel.getPatientMedicalInformation( );

            if( newInformation != null && !newInformation.equals( "" ) )
            {
                patient.changeMedicalInformation( newInformation );
                JOptionPane.showMessageDialog( this, "The patient's medical information has been updated", "Patient Search", JOptionPane.INFORMATION_MESSAGE );
            }
            else
            {
                JOptionPane.showMessageDialog( this, "The patient's medical information can't be empty", "Patient Search", JOptionPane.ERROR_MESSAGE );
            }
        }
    }
}
